<template>
  <el-dialog  @close='handleClose' :title="$t('group.bindOperator')" width='50%' bodyStyle="padding:0px;">
    <el-card
       
      shadow="never"
      :body-style="{padding: '0px'}"
      :page-size="pageSize"
      :total="rows"
      :current-page="pageNo"
      @current-change="currentChangeHandle"
    >
    <el-form class="p-4" :inline="true" :model="dataForm">
      <el-form-item>
        <el-input v-model="dataForm.operatorName" @blur="() => dataForm.operatorName = dataForm.operatorName.trim()"
                  @keyup.enter.native="getDataList(pageNo = 1)"
                  
                  :placeholder="$t('operator.condition')" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="w-8" type="primary"  @click="getDataList(pageNo = 1)"><svg-icon icon-class="Search"/></el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      v-loading="dataListLoading"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      stripe
      >
      <el-table-column
        type="selection"
        width="50">
      </el-table-column>
      <el-table-column
        prop="operatorName"
        
        
        :label="$t('group.operator.name')">
      </el-table-column>
      <el-table-column
        prop="linkMan"
        
       
        :label="$t('group.operator.linkMan')">
      </el-table-column>
      <el-table-column
        prop="email"
        
       
        :label="$t('group.operator.email')">
      </el-table-column>
      <el-table-column
        prop="creTime"
        
       
        :label="$t('group.operator.creTime')">
        <template v-slot="scope">
          <label>{{formatDate(scope.row.creTime, 'YYYY-MM-DD  HH:mm:ss')}}</label>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <template #footer>
    <div class='dialog-footer'>
      <span>
        <el-button text @click='handleClose'>{{$t('common.cancel')}}</el-button>
        <el-button type='primary' :loading="loading" @click="dataFormSubmit">{{$t('common.confirm')}}</el-button>
      </span>
    </div>
  </template>
  </el-dialog>
</template>

<script>
import {formatDate} from '@/utils/formatDate'
import {unBindOperatorPage, addgroupOperator} from '@/api/group'

export default {
  props: {
  },
  data () {
    return {
      loading: false,
      groupId: '',
      dataForm: {
        operatorName: '',
        appPkgName: '',
        pubKey: '',
        radio: {},
        quantity: 0,
        effectTimestamp: '',
        remark: ''
      },
      dataList: [],
      dataListSelections: [],
      dataListLoading: false,
      pageNo: 1,
      pageSize: 10,
      rows: 0
    }
  },
  created () {

  },
  methods: {
    init (id) {
      this.groupId = id
      this.dataForm.operatorName = ''
      this.loading = false
      this.dataListLoading = false
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageNo = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageNo = val
      this.getDataList()
    },
    formatDate (cellValue, fmt) {
      cellValue = new Date(cellValue).valueOf()
      return formatDate(cellValue, fmt)
    },
    getDataList () {
      this.loading = true
      let params = {
        operatorName: this.dataForm.operatorName,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }
      unBindOperatorPage(params).then(data => {
        console.log(data.data)
        this.dataList = data.data
        this.rows = data.rows
        this.loading = false
      }).catch((e) => {
        this.$confirmError(e)
        this.loading = false
      })
      this.dataListLoading = false
    },
    handleSelectionChange (val) {
      this.dataListSelections = val
    },
    handleClose () {
      this.$emit('update:model-value',false)
    },
    dataFormSubmit () {
      this.dataListLoading = true
      let operatorIds = []
      this.dataListSelections.map(i => {
        operatorIds.push(i.operatorId)
      })
      if (operatorIds.length === 0) {
        this.$message.error('Please select an operator')
        return false
      }
      let param = {
        operatorIds: operatorIds
      }
      addgroupOperator(this.groupId, param).then(data => {
        this.dataListLoading = false
        this.$emit('close')
        this.$emit('refreshDataList')
      }).catch((e) => {
        this.dataListLoading = false
        this.$confirmError(e)
        this.visible = true
      })
    }
  }
}
</script>
